<template>
  <div class="dialogContent">
    <el-dialog
      :visible.sync="dialogVisible"
      width="70%"
      class="newDialog"
      title="订单详情"
    >
      <div class="content">
        <el-steps
          :space="300"
          :active="stepsActive"
          align-center
          finish-status="success"
        >
          <el-step title="提交订单"></el-step>
          <el-step title="订单支付"></el-step>
          <el-step title="订单发货"></el-step>
          <el-step title="交易完成"></el-step>
        </el-steps>
        <div class="personInfo">
          <div class="orderInfo">
            <h4>订单信息</h4>
            <div class="row">
              <span class="rowlabel">订单编号:</span>
              <span class="rowvalue">{{ currentObject.orderNumber }}</span>
            </div>
            <div class="row">
              <span class="rowlabel">支付金额:</span>
              <span class="rowvalue"
                >{{
                  currentObject.totalPrice +
                  currentObject.freightPrice +
                  currentObject.servePrice
                }}元 (商品费用{{ currentObject.totalPrice }}元，快递费{{
                  currentObject.freightPrice
                }}元，服务费{{ currentObject.servePrice }}元）</span
              >
            </div>
            <div class="row">
              <span class="rowlabel">收货地址:</span>
              <span class="rowvalue">
                <p>
                  {{ currentObject.receiverName }}
                  {{ currentObject.receiverPhone }}
                </p>
                <p>
                  {{ currentObject.receiverProvince }}
                  - {{ currentObject.receiverCity }} -
                  {{ currentObject.receiverRegion }} -
                  {{ currentObject.receiverDetail }}
                </p>
              </span>
            </div>
          </div>
          <div class="orderStatus">
            <h4>
              订单当前状态：<span style="color: #399ef1">
                {{
                  currentObject.status == 0
                    ? "待付款"
                    : currentObject.status == 1
                    ? "待发货"
                    : currentObject.status == 2
                    ? "已发货"
                    : currentObject.status == 3
                    ? "已完成"
                    : currentObject.status == 4
                    ? "已关闭"
                    : currentObject.status == 5
                    ? "无效订单"
                    : currentObject.status == 6
                    ? "售后订单"
                    : currentObject.status == 7
                    ? "退款完成"
                    : ""
                }}</span
              >
            </h4>
            <div class="row">
              <span class="rowlabel">下单时间:</span>
              <span class="rowvalue">{{ currentObject.orderTime }}</span>
            </div>
          </div>
        </div>
        <div class="table">
          <div class="tableBox">
            <el-table
              :data="tableData"
              header-cell-class-name="tableHeader"
              style="width: 100%"
              height="35vh"
            >
              <el-table-column prop="date" label="商品" align="center">
                <template slot-scope="scope">
                  <div
                    class="shopsData"
                    @click="
                      $router.push({
                        path: '/detail',
                        query: {
                          id: scope.row.productId,
                        },
                      })
                    "
                  >
                    <img :src="scope.row.img" class="newImg" alt="" />
                    <div class="shopsDataBox">
                      <div class="nameBox" style="white-space: nowrap">
                        {{ scope.row.name }}
                      </div>
                      <!-- <div class="Model">商品货号：{{ scope.row.code }}</div>
                      <div class="Model">
                        颜色：{{ scope.row.sku.color }} 尺码：{{
                          scope.row.sku.size
                        }}
                      </div> -->
                    </div>
                  </div>
                </template>
              </el-table-column>
              <el-table-column prop="price" label="单价" align="center">
              </el-table-column>
              <el-table-column prop="quantity" label="数量" align="center">
              </el-table-column>
              <el-table-column prop="storeName" label="供应商" align="center">
              </el-table-column>
              <el-table-column prop="deliverySn" label="运单号" align="center">
              </el-table-column>
              <!-- 订单状态：0->待付款；1->待发货；2->已发货；3->已完成；4->已关闭；5->无效订单；6->售后订单 -->
              <!-- 商品状态(0未发货 1已发货 2已签收 3售后中) -->
              <!-- 商品状态(0未发货 1已发货 2已签收 3售后中 4退款中 5已完成) -->
              <el-table-column label="状态" align="center">
                <template slot-scope="scope">
                  <template v-if="currentObject.status == 0">待付款</template>
                  <template v-else>
                    {{
                      scope.row.status === 0
                        ? "未发货"
                        : scope.row.status === 1
                        ? "已发货"
                        : scope.row.status === 2
                        ? "已签收"
                        : scope.row.status === 3
                        ? "售后中"
                        : scope.row.status === 4
                        ? "退款中"
                        : scope.row.status === 5
                        ? "已完成"
                        : ""
                    }}
                  </template>
                </template>
              </el-table-column>
              <el-table-column fixed="right" label="操作" align="center">
                <template slot-scope="scope">
                  <el-button
                    v-if="
                      currentObject.status == 1 || currentObject.status == 2
                    "
                    type="text"
                    @click="handleApplyRefund(scope.row)"
                  >
                    申请退款
                  </el-button>
                  <el-button
                    v-if="currentObject.status != 0"
                    type="text"
                    @click="handleView(scope.row)"
                    >查看物流</el-button
                  >
                  <el-button
                    type="text"
                    v-if="scope.row.status == 1"
                    @click="handleReceive(scope.row)"
                    >确认收货</el-button
                  >
                </template>
              </el-table-column>
            </el-table>
          </div>
        </div>
      </div>
    </el-dialog>
    <el-drawer :title="'运单号：' + checkedLogisticsNumber" :visible.sync="dialogViewVisible">
      <el-timeline>
        <el-timeline-item
          v-for="(activity, index) in logisticsInformation"
          :key="index"
          :timestamp="activity.time"
        >
          {{ activity.context }}
        </el-timeline-item>
      </el-timeline>
    </el-drawer>
  </div>
</template>

<script>
import {
  getOrderDetail,
  postReceiveGoods,
  fetchExpressList,
} from "@/api/OrderManagement";

export default {
  name: "orderDetailDialog",
  data() {
    return {
      dialogVisible: false,
      dialogViewVisible: false,
      currentObject: {},
      tableData: [],
      logisticsInformation: [],
      checkedLogisticsNumber: ''
    };
  },
  created() {},
  computed: {
    stepsActive() {
      return this.currentObject == null
        ? 0
        : this.currentObject.status == 0
        ? 1
        : this.currentObject.status == 1
        ? 2
        : this.currentObject.status == 2
        ? 3
        : 4;
    },
  },
  methods: {
    handleView(i) {
      this.checkedLogisticsNumber = i.deliverySn
      fetchExpressList(i.id).then((res) => {
        if (res.code == 200) {
          if (res.data == null)
            return this.$message({ type: "error", message: "暂无物流信息" });
          this.logisticsInformation = res.data;
          this.dialogViewVisible = true;
        }
      });
    },
    handleApplyRefund(item) {
      this.$router.push({
        path: "/applyRefund",
        query: {
          goodsId: item.id,
        },
      });
    },

    handleReceive(item) {
      this.$confirm("确认收货吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        postReceiveGoods(item.id).then((res) => {
          this.$message({
            type: "success",
            message: "操作成功",
          });
          this.getOrderDetailChange(this.currentObject.id);
        });
      });
    },
    showDialog(show, row) {
      this.dialogVisible = show;
      this.currentObject = {};
      this.tableData = [];
      this.getOrderDetailChange(row.id);
    },

    getOrderDetailChange(id) {
      getOrderDetail(id).then((res) => {
        if (res.code == 200) {
          console.log(res.data);
          this.currentObject = res.data;
          this.currentObject.productDetailList.forEach((item1) => {
            item1.sku = JSON.parse(item1.sku);
          });
          this.tableData = this.currentObject.productDetailList;
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
/deep/.el-timeline {
  .el-timeline-item:first-child {
    .el-timeline-item__content,
    .el-timeline-item__timestamp {
      color: #07c160 !important;
    }
    .el-timeline-item__node {
      background: #07c160 !important;
    }
  }
}
.content {
  padding: 20px;
}
/deep/ .el-step__head.is-success {
  color: #e22a1f;
  border-color: #e22a1f;
}
/deep/ .el-step__title.is-success {
  color: #e22a1f;
}
.personInfo {
  display: flex;
  margin-top: 20px;
  h4 {
    padding: 0;
    margin-top: 0;
  }
  .row {
    margin-bottom: 25px;
    display: flex;
    align-items: center;
    .rowvalue {
      margin-left: 5px;
      p {
        margin: 5px;
      }
    }
  }
  .orderInfo {
    width: 50%;
    box-sizing: border-box;
    padding: 36px;
    background: #f5f7f9;
  }
  .orderStatus {
    flex: 1;
    box-sizing: border-box;
    padding: 36px;
  }
}
.tableBox {
  margin-top: 30px;
  /deep/ .tableHeader {
    background: #ebeff5;
    font-weight: 400;
    color: #000;
  }
  .shopsData {
    display: flex;
    .newImg {
      width: 80px;
      height: 80px;
      border-radius: 10px;
    }
    .shopsDataBox {
      margin-left: 10px;
      .nameBox {
        font-size: 14px;
      }
      .Model {
        color: #999999;
        margin-top: 8px;
        font-size: 12px;
      }
    }
  }
  .lvBox {
    color: #07c160;
  }
  .hongBox {
    color: #e22a1f;
  }
}
.pagingBox {
  display: flex;
  justify-content: center;
}
</style>
